package com.example.villagemarket.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.example.villagemarket.R;
import com.example.villagemarket.ShoppingCart;
import com.example.villagemarket.R.drawable;
import com.example.villagemarket.R.id;
import com.example.villagemarket.R.layout;
import com.haarman.listviewanimations.itemmanipulation.ExpandableListItemAdapter;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ResultsAdapter extends ExpandableListItemAdapter<HashMap<String, String>> {

	private Activity activity;
	private List<HashMap<String, String>> data;
	private static LayoutInflater inflater = null;
	Context context;
	HashMap<String, String> merchandise;
	
	//Holds items to add to the shopping cart
	ArrayList<String> tempHolderForShoppingCart;
	
	//Shopping cart object to manage the shopping cart
	ShoppingCart sc = new ShoppingCart();
	
	Menu m;
	
	
	public ResultsAdapter(Context c, List<HashMap<String, String>> items, Activity a, Menu m) {
		super(c, items);
		
		activity = a;
		data = items;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		context = c;
		
		merchandise = new HashMap<String, String>();
		
		setLimit(1);
		
		tempHolderForShoppingCart = new ArrayList<String>();
		
		this.m = m;
	}






	public int getCount() {
		return data.size();
	}

	public HashMap<String, String> getItem(HashMap<String, String> position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

    @Override
    public View getTitleView(int position, View convertView, ViewGroup parent) {
    	
    	View vi = convertView;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.results_item, null);
		}

		TextView description = (TextView) vi.findViewById(R.id.description);
		

		ImageView image = (ImageView) vi.findViewById(R.id.image);

		image.setTag(new Integer(position));


		merchandise = data.get(position);

		description.setText(merchandise.get(ResultListView.ITEM_DESCRIPTION));

		image.setImageResource(R.drawable.add_to_cart);

		image.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				//Adds the price, quanitiy, and description to a hash map
				//in the shopping cart class
				tempHolderForShoppingCart = new ArrayList<String>();
				//Set quantity to one
				tempHolderForShoppingCart.add(1+"");
				tempHolderForShoppingCart.add(data.get(Integer.parseInt(view.getTag().toString())).get("price"));
								
				ShoppingCart.items.put(data.get(Integer.parseInt(view.getTag().toString())).get("description"), tempHolderForShoppingCart);
				
				 
				sc.updateCart(context, activity, m);
			}
		});
		return vi;
    }
    


    @Override
    public View getContentView(int position, View convertView, ViewGroup parent) {
    	
    	View vi = convertView;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.results_expanded_item, null);
		}
		
		TextView price = (TextView) vi.findViewById(R.id.price);
		TextView quantity = (TextView) vi.findViewById(R.id.quantity);
		
		price.setText("Price: "+merchandise.get(ResultListView.ITEM_PRICE));
		quantity.setText("Amt: " +merchandise.get(ResultListView.ITEM_QUANTITY));

        return vi;
    }
}